package com.homegoods.home.presenters;

import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.homegoods.goods.adapter.FlexBoxAdapter;
import com.homegoods.home.HomeApiUtils;
import com.homegoods.home.model.NewProductModel;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.MyUtils;

import java.util.List;

import io.reactivex.functions.Consumer;


/**
 * @author Lxl
 * @date 2017/5/8
 * 搜索结果P层
 */
public class SearchResultsListPresenter extends AbstractRefreshPresenter<RefreshingView> {

    private String keyWork = "";
    private int pos = 1;
    private String myType = "";
    private String value = "";

    public SearchResultsListPresenter(RefreshingView view) {
        super(view);
    }

    public void updateContent(String keyWork) {
        this.keyWork = keyWork;
        refreshData();
    }

    public void setKeyWork(String keyWork) {
        this.keyWork = keyWork;
    }

    public void setMyType(String type, String value) {
        this.myType = type;
        this.value = value;
    }

    @Override
    protected void initItems() {
        super.initItems();
    }

    @Override
    protected void fetchData(final boolean loadMore) {
        if (loadMore) {
            pos++;
        }
        //modify shibo.zheng test
        HomeApiUtils.getClassTreeCommodity(/*keyWork*/"factory_4c1422f0511842189f83bcc26d0d73a5", pos, myType, value, new Consumer<SubModelList>() {
            @Override
            public void accept(SubModelList subModelList) throws Exception {
                List<NewProductModel> list = JSON.parseArray(subModelList.getItems(), NewProductModel.class);
                if (list != null) {
                    SearchResultsListPresenter.this.parserData(loadMore, list);
                }
            }
        }, throwable -> {

        });
    }

    @Override
    public MultiTypeAdapter getAdapter() {
        adapter = new FlexBoxAdapter(mView.getContext(), 2, MyUtils.dp2px(8));
        return adapter;
    }
}
